const Service = require('egg').Service

class HomeService extends Service {
    async getHomeData() {
        const firstListUrl = this.config.news.firstListUrl
        const catUrl = this.config.news.catUrl
        const sliderUrl = this.config.news.sliderUrl
        const firstListData = async () => {
            const data = await this.ctx.curl(firstListUrl, {
                dataType: 'json'
            })
            return data
        }
        const catData = async () => {
            const data = await this.ctx.curl(catUrl, {
                dataType: 'json'
            })
            return data
        }
        const sliderData = async () => {
            const data = await this.ctx.curl(sliderUrl, {
                dataType: 'json'
            })
            return data
        }
        const finalData = await Promise.all([firstListData(), catData(), sliderData()])
        return {
            firstList: finalData[0].data.data,
            catList: Object.values(finalData[1].data.data),
            sliderList: finalData[2].data.data,
        }
    }
}

module.exports = HomeService